home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9630 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  107 lines

  1. Path: titan.fullerton.edu!grosin
  2. From: grosin@titan.fullerton.edu (Gil Rosin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Please help w/ Stack implementation
  5. Date: 3 Mar 1996 06:32:40 GMT
  6. Organization: California State University at Fullerton
  7. Message-ID: <4hbee8$b6h@wintermute.ecs.fullerton.edu>
  8. NNTP-Posting-Host: titan.ecs.fullerton.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. Hi, I'm learning C++ and am doing a Stack class to help learn some features of
  12. C++. I am using templates for generic a generic stack.
  13.  
  14. I am also using reference functions for Pop() and TopElement() which pop and
  15. return the top element as well as just return the top element respectively.
  16.  
  17. Now, currently, my "node record" is defined as:
  18.  
  19. struct Node
  20.  {
  21.   ElementType Element;
  22.   Node *Next;
  23.  };
  24.  
  25. Now, for TopElement, its simple, I just return the crap at the head of the
  26. stack and don't have to worry about it going out of existance.
  27.  
  28. But how do I handle Pop()?
  29.  
  30. Currently I have it defined as:
  31.  
  32. template <class ElementType>
  33.  
  34. ElementType & Stack<ElementType>::Pop()
  35.  
  36.  {
  37.  
  38.   Node *TempNode;                                       //
  39.  
  40.   ElementType & TempElement = TopOfStack->Element;      //
  41.  
  42.  
  43.  
  44.   cout << &TempElement << '\n';
  45.  
  46.   cout << &TopOfStack->Element << '\n';
  47.  
  48.                                                         //
  49.  
  50.   if (ElementsInStack)                                  //
  51.  
  52.     {                                                   //
  53.  
  54.      TempNode = TopOfStack->NextNode;                   //
  55.  
  56.      delete TopOfStack;                                 //
  57.  
  58.      TopOfStack = TempNode;                             //
  59.  
  60.      ElementsInStack--;                                 //
  61.  
  62.      return TempElement;                                //
  63.  
  64.     }                                                   //
  65.  
  66.   else                                                  //
  67.  
  68.      cout << "Error: Stack is empty." << '\n';          //
  69.  
  70.   return TempElement;                                   //
  71.  
  72.  }
  73.  
  74.  
  75. Ok, now as you can see in this function. I return TempElement at the end,
  76. this is incorrect right? Since the damn thing is DELETED!
  77.  
  78. How can I handle this? I thought about having TempElement as a pointer
  79. instead, but then I have a stray memory allocation lying around. Lets say I
  80. went this route, how would I clean up all the allocated memory left over
  81. from popping the items?
  82.  
  83. I also looked at Borland C++'s stack class and they have a Node record
  84. declared somewhat like mine, except Element is a pointer and each element
  85. is allocated memory.
  86.  
  87. Then when they pop it, they detach it from the list, they delete the actual
  88. node record, but leave the element allocated. So again, you have stray
  89. memory left over.
  90.  
  91. Now my question is how do I handle that allocated memory from elements if I
  92. don't delete them, but delete the only pointer to them and remove them from
  93. the stack?
  94.  
  95. Of course the user can delete them, but that is a cumbersome chore, I mean,
  96. if I wanted to simply print out the value, I would need minimum two lines,
  97. one to Read the element with TopElement and one to pop it with Pop.
  98.  
  99. Can some one give me some information on this, like I said, I am new to C++,
  100. so I might be missing where the clean up is done and how.
  101.  
  102. P.S., this is NOT homework, I am doing this for myself.
  103.  
  104. Please reply via email to grosin@titan.fullerton.edu
  105.  
  106.  
  107.